package com.bfsuol.bfsuolframework.core.service;

import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuol.bfsuolframework.core.dao.DAO;
import com.bfsuol.bfsuolframework.core.dao.support.HqlParameter;
import com.bfsuol.bfsuolframework.core.entity.Entity;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public abstract class ServiceSupport<E extends Entity> implements Service<E> {
	/**
	 * Logger for this class
	 */
	protected final Logger logger = Logger.getLogger(getClass());
	
	public ServiceSupport(){	}

	public abstract DAO<E> getDao();

	public Long save(E obj) {
		return getDao().save(obj);
	}
	
	public void update(E obj) {
		getDao().update(obj);
	}
	
	public Long saveOrUpdate(E obj) {
		return getDao().saveOrUpdate(obj);
	}
	
	public void merge(E obj) {
		getDao().merge(obj);
	}

	public E getById(Long id) {
		return (E)getDao().getById(id);
	}

	public void delete(E obj) {
		getDao().delete(obj);
	}
	
	public void delete(Long id) {
		getDao().delete(id);
	}
	
	public void evict(E obj) {
		getDao().evict(obj);
	}

	public void clear(){
		getDao().clear();
	}
	
	public List<E> findAll() {
		return getDao().findAll();
	}
	
	public int getCount(QueryParam query) {
		return getDao().getCount(query);
	}

	public List<E> findAll(QueryParam query) {
		return getDao().findAll(query);
	}

	public List<E> findAll(QueryParam query, int startRecord,int pageSize) {
		return getDao().findAll(query, startRecord, pageSize);
	}

	public Pagination getPage(QueryParam query, Pagination page) {
		return getDao().getPage(query, page);
	}
	
	public Pagination getPage(String hql, HqlParameter parameters,Pagination page)
	{
		return getDao().getPage(hql, parameters, page);
	}
	
	public boolean isDeletable(Long id){
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.bfsuolframework.core.service.Service#isPropertyUnique(java.lang.String, java.lang.Object)
	 */
	public boolean isPropertyUnique(final String propertyName, final Object value){
		return getDao().isPropertyUnique(propertyName, value, null);
	}
	
	/* (non-Javadoc)
	 * @see com.bfsuolframework.core.service.Service#isPropertyUnique(java.lang.String, java.lang.Object, java.lang.Long)
	 */
	public boolean isPropertyUnique(final String propertyName, final Object value, final Long excludePrimaryKey){
		return getDao().isPropertyUnique(propertyName, value, excludePrimaryKey);
	}
	
}
